﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ScannerWatcher.Service
{
    public static class Settings
    {
        public static string ServerAddress { get; private set; }
        public static string ScanPath { get; set; }

        public static string API_GetOngoingCases = "api/CaseInfo/GetOngoingCases";
        public static string API_GetCaseTreeChildNodes = "api/CaseInfo/GetCaseTreeChildNodes";
        public static string API_UploadAttachFile = "api/CaseInfo/UploadAttachFile";
        public static string API_EnsureCaseTreeExist = "api/CaseInfo/EnsureCaseTreeExist";
        public static string API_AddCaseTree = "api/CaseInfo/AddCaseTree";
        public static string API_GetCaseUndefinedCategory = "api/CaseInfo/GetCaseUndefinedCategory";
        public static string API_AddFactAttach = "api/CaseInfo/AddFactAttach";

        static Settings()
        {
            ServerAddress = System.Configuration.ConfigurationManager.AppSettings["ServerAddress"];
            ScanPath = System.Configuration.ConfigurationManager.AppSettings["ScanPath"];

            Load();
        }

        public static void Load()
        {
            string path = System.IO.Path.Combine(Application.StartupPath, "config.json");
            if (System.IO.File.Exists(path))
            {
                string jsonStr = System.IO.File.ReadAllText(path);
                JsonTextReader reader = new JsonTextReader(new StringReader(jsonStr));
                JObject obj = JObject.Load(reader);
                reader.Close();

                ServerAddress = obj["ServerAddress"].Value<string>();
                ScanPath = obj["ScanPath"].Value<string>();
            }

            API_GetOngoingCases = ServerAddress + API_GetOngoingCases;
            API_GetCaseTreeChildNodes = ServerAddress + API_GetCaseTreeChildNodes;
            API_UploadAttachFile = ServerAddress + API_UploadAttachFile;
            API_EnsureCaseTreeExist = ServerAddress + API_EnsureCaseTreeExist;
            API_AddCaseTree = ServerAddress + API_AddCaseTree;
            API_GetCaseUndefinedCategory = ServerAddress + API_GetCaseUndefinedCategory;
            API_AddFactAttach = ServerAddress + API_AddFactAttach;
        }

        public static void Save()
        {
            JObject obj = new JObject();
            obj.Add("ServerAddress", ServerAddress);
            obj.Add("ScanPath", ScanPath);
            string jsonStr = obj.ToString();
            string path = System.IO.Path.Combine(Application.StartupPath, "config.json");
            System.IO.File.WriteAllText(path, jsonStr);
        }
    }
}
